package com.ruoyi.web.controller.system;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.chuanglan.SMSConst;
import com.chuanglan.sms.util.ChuangLanSmsUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.enums.BusinessStatus;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.CardBinNotFoundException;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.utils.Constans;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.system.domain.HatchetAcq;
import com.ruoyi.system.domain.HatchetAgent;
import com.ruoyi.system.domain.HatchetCardBin;
import com.ruoyi.system.domain.HatchetIssBank;
import com.ruoyi.system.domain.HatchetMerchant;
import com.ruoyi.system.domain.HatchetMerchantIncreaseLimit;
import com.ruoyi.system.domain.HatchetMoneyChange;
import com.ruoyi.system.domain.HatchetPlan;
import com.ruoyi.system.domain.HatchetPlanItem;
import com.ruoyi.system.domain.IosMsg;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.IHatchetAcqService;
import com.ruoyi.system.service.IHatchetAgentService;
import com.ruoyi.system.service.IHatchetCardBinService;
import com.ruoyi.system.service.IHatchetIssBankService;
import com.ruoyi.system.service.IHatchetMerchantIncreaseLimitService;
import com.ruoyi.system.service.IHatchetMerchantService;
import com.ruoyi.system.service.IHatchetMoneyChangeService;
import com.ruoyi.system.service.IHatchetPlanItemService;
import com.ruoyi.system.service.IHatchetPlanService;
import com.ruoyi.system.service.IIosMsgService;
import com.ruoyi.system.service.INewsService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.vo.SecurityUtils;
import com.ruoyi.system.vo.weishuapay.CityUtil;

import net.sf.json.JSONObject;

/**
 * 还款计划 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-03-01
 */
@Controller
@RequestMapping("/system/hatchetPlan")
public class HatchetPlanController extends BaseController {
	private String prefix = "system/hatchetPlan";

	@Autowired
	private IHatchetPlanService hatchetPlanService;
	@Autowired
	private IHatchetPlanItemService itemService;
	@Autowired
	private IHatchetAcqService acqService;
	@Autowired
	private IHatchetMerchantIncreaseLimitService increaseLimitService;
	@Autowired
	private IHatchetMerchantService merchantService;
	@Autowired
	private IHatchetCardBinService binService;
	@Autowired
	private IHatchetIssBankService issBankService;
	@Autowired
	private IHatchetMoneyChangeService moneyChangeService;
	@Autowired
	private IHatchetAgentService agentService;
	@Autowired
	private INewsService newsService;
	@Autowired
	private IIosMsgService iosMsgService;
	@Autowired
	private ISysUserService userService;

	@RequiresPermissions("system:hatchetPlan:view")
	@GetMapping("/1") // 大计划
	public String hatchetPlan(ModelMap mmap) {
		/* systemUserService.updatePassWord(); */
		return prefix + "/hatchetPlan";
	}

	@GetMapping("/2") // 计划处理
	public String doPlan() {
		return prefix + "/doPlan";
	}

	/**
	 * 查询还款计划列表
	 */
	@RequiresPermissions("system:hatchetPlan:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(HatchetPlan hatchetPlan) {
		// 判断顾问编号不为空
		if (StringUtils.isNotEmpty(hatchetPlan.getBrokerId())) {
			HatchetMerchant merchantByNo = merchantService.selectHatchetMerchantByMerchantNo(hatchetPlan.getBrokerId());
			// 通过顾问的商户号查对应的顾问商户id再去查询高级顾问制定的计划
			if (StringUtils.isNotNull(merchantByNo)) {
				hatchetPlan.setBrokerId(merchantByNo.getID());
			}
		}
		BigDecimal sumMoney = hatchetPlanService.selectPlanSumMoney(hatchetPlan);
		startPage();
		// 判断商名称和商户编号是否为空
		List<HatchetPlan> list = hatchetPlanService.selectHatchetPlanList(hatchetPlan);
		int index = 0;
		for (HatchetPlan hatchetPlan2 : list) {
			if (index == 0) {
				hatchetPlan2.setSumMoney(sumMoney);
				index++;
			}
			// 2）封装通道名称
			if (StringUtils.isNotEmpty(hatchetPlan2.getAcqCode())) {
				HatchetAcq acq = acqService.selectHatchetByAcqCode(hatchetPlan2.getAcqCode());
				if (acq != null) {
					hatchetPlan2.setAcqName(acq.getAcqName());
				}
			}
			// 3)封装银行卡卡号和银行名称 ，排除智能收款的情况
			HatchetPlanItem item = new HatchetPlanItem();
			item.setPlanId(hatchetPlan2.getID());
			List<HatchetPlanItem> itemList = itemService.selectHatchetPlanItemList(item);
			if (itemList.size() > 2) {
				HatchetPlan hatchetPlan3 = hatchetPlanService.selectBankNameById(hatchetPlan2.getID());
				if (hatchetPlan3 != null) {
					hatchetPlan2.setBankName(hatchetPlan3.getBankName());
					hatchetPlan2.setCardNo(hatchetPlan3.getCardNo());
				}

			}
			// 4）封装制定计划的高级顾问的商户号
			if (StringUtils.isNotEmpty(hatchetPlan2.getBrokerId())) {
				HatchetMerchant merchant = merchantService.selectHatchetMerchantById(hatchetPlan2.getBrokerId());
				if (StringUtils.isNotNull(merchant)) {
					// 借用brokerId 字段封装高级顾问商户号
					hatchetPlan2.setBrokerId(merchant.getMerchantNo());
				}
			}
		}
		return getDataTable(list);
	}

	/**
	 * 导出还款计划列表
	 */
	@RequiresPermissions("system:hatchetPlan:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(HatchetPlan hatchetPlan) {
		List<HatchetPlan> list = hatchetPlanService.selectHatchetPlanList(hatchetPlan);
		ExcelUtil<HatchetPlan> util = new ExcelUtil<HatchetPlan>(HatchetPlan.class);
		return util.exportExcel(list, "hatchetPlan");
	}

	/**
	 * 新增还款计划
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存还款计划
	 */
	@RequiresPermissions("system:hatchetPlan:add")
	@Log(title = "还款计划", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(HatchetPlan hatchetPlan) {
		return toAjax(hatchetPlanService.insertHatchetPlan(hatchetPlan));
	}

	/**
	 * 修改还款计划
	 */
	@GetMapping("/edit/{iD}")
	public String edit(@PathVariable("iD") String iD, ModelMap mmap) {
		HatchetPlan hatchetPlan = hatchetPlanService.selectHatchetPlanById(iD);
		mmap.put("hatchetPlan", hatchetPlan);
		return prefix + "/edit";
	}

	/**
	 * 修改保存还款计划
	 */
	@RequiresPermissions("system:hatchetPlan:edit")
	@Log(title = "还款计划", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(HatchetPlan hatchetPlan) {
		return toAjax(hatchetPlanService.updateHatchetPlan(hatchetPlan));
	}

	/**
	 * 删除还款计划
	 */
	@RequiresPermissions("system:hatchetPlan:remove")
	@Log(title = "还款计划", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(hatchetPlanService.deleteHatchetPlanByIds(ids));
	}

	/**
	 * 计划启用和禁用
	 * 
	 * @param increaseLimit
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/changeStatus")
	public AjaxResult changeStatus(HatchetPlan hatchetPlan) {
		return toAjax(hatchetPlanService.updateHatchetPlan(hatchetPlan));
	}

	// 计划拉起操作
	@Log(title = "拉起计划", businessType = BusinessType.UPDATE)
	@ResponseBody
	@RequestMapping("/laqiPlan")
	public Map<String, Object> laqiPlan(@RequestParam("planItemId") String planItemId) {
		Map<String, Object> map = new HashMap<>();
		HatchetPlanItem item = itemService.selectHatchetPlanItemById(planItemId);
		if (StringUtils.isNull(item)) {
			map.put("msg", "子计划无法查到");
			map.put("status", BusinessStatus.FAIL);
			return map;
		}
		HatchetPlan plan = hatchetPlanService.selectHatchetPlanById(item.getPlanId());
		if ("-1".equals(plan.getSTATUS())) {
			map.put("msg", "大计划已被客户删除，无法暴力拉起！");
			map.put("status", BusinessStatus.FAIL);
			return map;
		}
		item.setSTATUS("10A");
		int updateHatchetPlanItem = itemService.updateHatchetPlanItem(item);
		if (updateHatchetPlanItem > 0) {
			map.put("status", BusinessStatus.SUCCESS);
			map.put("msg", "成功拉起子计划");
			return map;
		} else {
			map.put("msg", "子计划拉起失败，请重新拉起！");
			map.put("status", BusinessStatus.FAIL);
			return map;
		}
	}

	// 立即执行计划
	@Log(title = "立即执行计划", businessType = BusinessType.UPDATE)
	@ResponseBody
	@RequestMapping("/doPlanNow")
	public Map<String, Object> doPlanNow(@RequestParam("planItemId") String planItemId) {
		Map<String, Object> map = new HashMap<>();
		HatchetPlanItem item = itemService.selectHatchetPlanItemById(planItemId);
		if (StringUtils.isNull(item)) {
			map.put("msg", "小计划无法查到");
			map.put("status", BusinessStatus.FAIL);
			return map;
		}
		// 修改小计划的执行时间为当前时间
		item.setPlanTime(new Date());
		int updateHatchetPlanItem = itemService.updateHatchetPlanItem(item);
		if (updateHatchetPlanItem > 0) {
			map.put("status", BusinessStatus.SUCCESS);
			map.put("msg", "成功修改小计划的执行时间");
			return map;
		} else {
			map.put("msg", "小计划的执行时间修改失败，请重新点击 立即执行 ！");
			map.put("status", BusinessStatus.FAIL);
			return map;
		}
	}

	// 完成计划操作
	@Log(title = "完成计划", businessType = BusinessType.UPDATE)
	@ResponseBody
	@RequestMapping("/completePlan")
	public Map<String, Object> completePlan(@RequestParam("planItemId") String planItemId) {
		Map<String, Object> map = new HashMap<>();
		try {
			map = itemService.completePlanItem(planItemId);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("msg", e.getMessage());
			map.put("status", BusinessStatus.FAIL);
			return map;
		}
		return map;
	}

	// 刷新用户操作
	@ResponseBody
	@RequestMapping("/refreshMer")
	public Map<String, Object> refreshMer(@RequestParam("merNo") String merNo) {
		Map<String, Object> map = new HashMap<>();
		// HatchetMerchant merchant =
		// merchantService.selectHatchetMerchantByMerchantNo(merNo);

		HatchetMerchant merchant = merchantService.selectHatchetMerchantByNo(merNo);

		if (StringUtils.isNotNull(merchant)) {
			String params = "uid=" + merchant.getID() + "&sign=" + StringUtils.getSign();
			String result = HttpUtils.sendGet(Constans.REFRESH_MER_URL, params);
			if (StringUtils.isEmpty(result)) {
				map.put("status", BusinessStatus.FAIL);
				map.put("msg", "刷新用户信息到缓存失败导致用户修改失败,请稍后再试");
			}
			JSONObject fromObject = JSONObject.fromObject(result);
			if ("0".equals(fromObject.getString("apistatus"))) {
				map.put("status", BusinessStatus.FAIL);
				map.put("msg", "刷新用户信息到缓存失败导致用户修改失败,请稍后再试");
			} else if ("1".equals(fromObject.getString("apistatus"))) {
				map.put("status", BusinessStatus.SUCCESS);
				map.put("msg", fromObject.get("apimsg"));
			}
		} else {
			map.put("status", BusinessStatus.FAIL);
			map.put("msg", "没有找到该用户，请确认您输入的用户编号是否正确");
		}
		return map;
	}

	/**
	 * 通过银行卡号查询信用卡信息
	 * 
	 * @param cardNo
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/selectCreditCard")
	public Map<String, Object> selectCreditCard(@RequestParam("cardNo") String cardNo) {
		HatchetMerchantIncreaseLimit increaseLimit = increaseLimitService
				.selectHatchetMerchantIncreaseLimitByCardNo(cardNo);
		Map<String, Object> map = new HashMap<>();
		if (StringUtils.isNull(increaseLimit)) {
			map.put("msg", "该银行卡信息不存在");
			map.put("status", BusinessStatus.FAIL);
			return map;
		}
		map.put("increaseLimit", increaseLimit);
		map.put("status", BusinessStatus.SUCCESS);
		return map;
	}

	/**
	 * 修改银行卡信息
	 * 
	 * @param increaseLimit
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/editCreditCard")
	public Map<String, Object> editCreditCard(HatchetMerchantIncreaseLimit increaseLimit) {
		int update = increaseLimitService.updateHatchetMerchantIncreaseLimit(increaseLimit);
		Map<String, Object> map = new HashMap<>();
		if (update > 0) {
			map.put("msg", "修改成功");
			map.put("status", BusinessStatus.SUCCESS);
			return map;
		} else {
			map.put("msg", "修改失败");
			map.put("status", BusinessStatus.FAIL);
			return map;
		}
	}

	/**
	 * 添加信用卡信息
	 * 
	 * @param increaseLimit
	 * @return
	 * @throws CardBinNotFoundException
	 */
	@ResponseBody
	@RequestMapping("/addCreditCard")
	public Map<String, Object> addCreditCard(HatchetMerchantIncreaseLimit increaseLimit)
			throws CardBinNotFoundException {
		Map<String, Object> map = new HashMap<>();
		increaseLimit.setID(StringUtils.randomUUID());
		increaseLimit.setCreateTime(new Date());
		HatchetMerchantIncreaseLimit paramIncreaseLimit = new HatchetMerchantIncreaseLimit();
		paramIncreaseLimit.setIncreaseLimitStatus("10B");// 未删除
		paramIncreaseLimit.setBankAccount(increaseLimit.getBankAccount());
		List<HatchetMerchantIncreaseLimit> selectHatchetMerchantIncreaseLimitList = increaseLimitService
				.selectHatchetMerchantIncreaseLimitList(paramIncreaseLimit);
		if (StringUtils.isNotEmpty(selectHatchetMerchantIncreaseLimitList)) {
			map.put("msg", "该张卡已绑定，请换张卡");
			map.put("status", BusinessStatus.FAIL);
			return map;
		}

		HatchetMerchant merchant = null;
		HatchetMerchant paramMerchant = new HatchetMerchant();
		// 通过身份证号查询用户信息
		if (StringUtils.isNotEmpty(increaseLimit.getIdCardNumber())) {
			paramMerchant.setIdCardNumber(increaseLimit.getIdCardNumber());
			List<HatchetMerchant> merchantList = merchantService.selectHatchetMerchantList(paramMerchant);
			if (StringUtils.isNotEmpty(merchantList)) {
				merchant = merchantList.get(0);
			}
		} else {
			map.put("msg", "该用户未认证");
			map.put("status", BusinessStatus.FAIL);
			return map;
		}
		HatchetCardBin cardBin = new HatchetCardBin();
		cardBin.setCardName(increaseLimit.getBankAccount());
		cardBin.setCardNoLen(increaseLimit.getBankAccount().length());
		List<HatchetCardBin> selectCardBinList = binService.selectCardBinList(cardBin);
		HatchetCardBin bin = null;
		if (selectCardBinList != null && selectCardBinList.size() > 0) {
			bin = selectCardBinList.get(0);
			HatchetIssBank bank = issBankService.selectHatchetIssBankById(bin.getBankId());
			if (null != bank) {
				increaseLimit.setMerchantId(merchant.getID());
				increaseLimit.setBankName(bank.getBankCode());
				increaseLimit.setBankAccountName(merchant.getBankAccountName());
				Date date = new Date();
				increaseLimit.setSubmitExamineTime(date);
				increaseLimit.setExaminePassTime(date);
				increaseLimit.setAuditRecord("审核日志");
				increaseLimit.setIncreaseLimitStatus("10B");
				int insert = increaseLimitService.insertHatchetMerchantIncreaseLimit(increaseLimit);
				if (insert > 0) {
					map.put("msg", "绑卡成功成功");
					map.put("status", BusinessStatus.SUCCESS);
					return map;
				} else {
					map.put("msg", "绑卡失败");
					map.put("status", BusinessStatus.FAIL);
					return map;
				}
			} else {
				map.put("msg", "银行信息没找到 ");
				map.put("status", BusinessStatus.FAIL);
				return map;
			}
		} else {
			map.put("msg", "卡bin没找到");
			map.put("status", BusinessStatus.FAIL);
			return map;
		}

	}

	/**
	 * 刷新城市 X8087 和Y 8063通道城市
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/flushCity")
	public void flushCity() {
		CityUtil.loadYxCityAndProducts("0", 8087 + "");
		CityUtil.loadYxCityAndProducts("0", 8063 + "");
	}

	/**
	 * 刷新新闻
	 * 
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/flushNews")
	public void getNews() throws Exception {
		newsService.flushNews();
	}

	/**
	 * 刷新app商品
	 * 
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/flushGoods")
	public void flushGoods() throws Exception {
		// 调用接口返回数据更新数据
		String params = "r=Port/index&type=total&appkey=5cff17fe6f&v=2&page=1";
		String url = "http://api.dataoke.com/index.php";
		IosMsg iosMsg = new IosMsg();
		iosMsg.setAcqId("800020");
		List<IosMsg> msgList = iosMsgService.selectIosMsgList(iosMsg);
		if (msgList.size() < 0) {
			String result = com.ruoyi.common.utils.HttpUtil1.sendGetMethod(url, params, "utf-8");
			Map<String, Object> mapTypes = JSON.parseObject(result);
			if (mapTypes == null || mapTypes.isEmpty()) {
			} else {
				String result1 = mapTypes.get("result").toString();
				iosMsg.setContent(result1);
				iosMsg.setCreateTime(new Date());
				iosMsg.setId(StringUtils.randomUUID() + "");
				iosMsgService.insertIosMsg(iosMsg);
			}
		} else {
			String result = com.ruoyi.common.utils.HttpUtil1.sendGetMethod(url, params, "utf-8");
			Map<String, Object> mapTypes = JSON.parseObject(result);
			if (mapTypes == null || mapTypes.isEmpty()) {
			} else {
				String result1 = mapTypes.get("result").toString();
				iosMsg.setContent(result1);
				iosMsg.setCreateTime(new Date());
				iosMsgService.updateIosMsg(iosMsg);
			}
		}

		IosMsg iosMsgTop = new IosMsg();
		iosMsg.setAcqId("800021");
		String paramsTop = "r=Port/index&type=top100&appkey=5cff17fe6f&v=2";
		List<IosMsg> topMsgList = iosMsgService.selectIosMsgList(iosMsgTop);
		if (topMsgList.size() < 0) {
			String result = com.ruoyi.common.utils.HttpUtil1.sendGetMethod(url, paramsTop, "utf-8");
			Map<String, Object> mapTypes = JSON.parseObject(result);
			if (mapTypes == null || mapTypes.isEmpty()) {
			} else {
				String result1 = mapTypes.get("result").toString();
				iosMsgTop.setContent(result1);
				iosMsgTop.setCreateTime(new Date());
				iosMsgTop.setId(StringUtils.randomUUID() + "");
				iosMsgService.insertIosMsg(iosMsgTop);
			}
		} else {
			String result = com.ruoyi.common.utils.HttpUtil1.sendGetMethod(url, paramsTop, "utf-8");
			Map<String, Object> mapTypes = JSON.parseObject(result);
			if (mapTypes == null || mapTypes.isEmpty()) {
			} else {
				String result1 = mapTypes.get("result").toString();
				iosMsgTop.setContent(result1);
				iosMsgTop.setCreateTime(new Date());
				iosMsgService.updateIosMsg(iosMsgTop);
			}
		}

		IosMsg paoliangMsg = new IosMsg();
		paoliangMsg.setAcqId("800022");
		String paramsPaoliang = "r=Port/index&type=paoliang&appkey=5cff17fe6f&v=2";
		List<IosMsg> paoliangList = iosMsgService.selectIosMsgList(paoliangMsg);
		if (paoliangList.size() < 0) {
			String result = com.ruoyi.common.utils.HttpUtil1.sendGetMethod(url, paramsPaoliang, "utf-8");
			Map<String, Object> mapTypes = JSON.parseObject(result);
			if (mapTypes == null || mapTypes.isEmpty()) {
			} else {
				String result1 = mapTypes.get("result").toString();
				paoliangMsg.setContent(result1);
				paoliangMsg.setCreateTime(new Date());
				paoliangMsg.setId(StringUtils.randomUUID() + "");
				iosMsgService.insertIosMsg(paoliangMsg);
			}
		} else {
			String result = com.ruoyi.common.utils.HttpUtil1.sendGetMethod(url, paramsPaoliang, "utf-8");
			Map<String, Object> mapTypes = JSON.parseObject(result);
			if (mapTypes == null || mapTypes.isEmpty()) {
			} else {
				String result1 = mapTypes.get("result").toString();
				paoliangMsg.setContent(result1);
				paoliangMsg.setCreateTime(new Date());
				iosMsgService.updateIosMsg(paoliangMsg);
			}
		}

	}

	/**
	 * 增加分润
	 * 
	 * @param increaseLimit
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/refund")
	@Transactional
	public Map<String, Object> refund(@RequestParam("orderNo1") String orderNo1,
			@RequestParam("merchantNo1") String merchantNo1, @RequestParam("money1") String money1) {
		Map<String, Object> map = new HashMap<>();
		HatchetMoneyChange hatchetMoneyChange = new HatchetMoneyChange();
		hatchetMoneyChange.setPaymentOrderId(orderNo1);
		List<HatchetMoneyChange> list = moneyChangeService.selectHatchetMoneyChangeList(hatchetMoneyChange);
		// 获取moneyType
		if (list.size() < 1) {
			map.put("msg", "修改失败,没有分润记录！");
			map.put("status", BusinessStatus.FAIL);
			return map;
		}
		hatchetMoneyChange.setID(StringUtils.randomUUID() + "");
		hatchetMoneyChange.setUserType(list.get(0).getUserType());
		hatchetMoneyChange.setMoneyType(list.get(0).getMoneyType());
		hatchetMoneyChange.setTrxAmt(new BigDecimal(money1));
		hatchetMoneyChange.setUserId(merchantNo1);
		hatchetMoneyChange.setPaymentOrderId(orderNo1);
		hatchetMoneyChange.setSTATUS("10B");
		hatchetMoneyChange.setCreateTime(new Date());
		int update = moneyChangeService.insertHatchetMoneyChange(hatchetMoneyChange);
		if (update > 0) {
			// 添加分润
			if (merchantNo1.length() > 7) {
				HatchetMerchant merchant = merchantService.selectHatchetMerchantByMerchantNo(merchantNo1);
				merchant.setTotalMoney(merchant.getTotalMoney().add(new BigDecimal(money1)));
				merchantService.updateHatchetMerchant(merchant);
			} else {
				HatchetAgent agent = agentService.selectHatchetAgentByNo(merchantNo1);
				agent.setTotalMoney(agent.getTotalMoney().add(new BigDecimal(money1)));
				agentService.updateHatchetAgent(agent);
			}

			map.put("msg", "修改成功");
			map.put("status", BusinessStatus.SUCCESS);
			return map;
		} else {
			map.put("msg", "修改失败");
			map.put("status", BusinessStatus.FAIL);
			return map;
		}
	}

	/**
	 * 增加分润
	 * 
	 * @param increaseLimit
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateAgentPassword")
	@Transactional
	public Map<String, Object> updateAgentPassword() {
		Map<String, Object> map = new HashMap<>();
		SysUser user = new SysUser();
		List<SysUser> list = userService.selectUserList(user);
		String password = SecurityUtils.EncoderByMd5("123456");
		String formate = "%s,%s";
		for (SysUser user1 : list) {
			String OldpassWord = user1.getPassword();
			if (password.equals(OldpassWord)) {
				HatchetAgent agent = agentService.selectHatchetAgentByNo(user1.getLoginName());
				if (agent != null) {
					String s = agent.getIdCardNo();
					String newpassWord = s.substring(s.length() - 8, s.length());
					user1.setPassword(SecurityUtils.EncoderByMd5(newpassWord));
					int a = userService.updateUser(user1);
					if (a > 0) {
						String params = String.format(formate, agent.getCorpPhone(), agent.getAgentNo());
						ChuangLanSmsUtil.sendSMSVar(SMSConst.pass_to_agent, params);
						System.out.println("更新密码得代理商=" + agent.getAgentName());
					} else {
						System.out.println("未更新密码得代理商=" + agent.getAgentName());
					}
				} else {
					System.out.println(user1.getLoginName());
				}
			}

		}
		map.put("msg", "修改成功");
		map.put("status", BusinessStatus.SUCCESS);
		return map;

	}

	/**
	 * 所有用户对账
	 * 
	 * @param increaseLimit
	 * @return
	 * @throws ParseException
	 */
	@ResponseBody
	@RequestMapping("/checkMoneyChange")
	@Transactional
	public Map<String, Object> checkMoneyChange() throws ParseException {
		Map<String, Object> params = new HashMap<>();
		List<HatchetAgent> agents = agentService.selectAllAgent();
		for (HatchetAgent hatchetAgent : agents) {

			// 总分润
			BigDecimal sumFenrun = moneyChangeService.selectSumFenRenChangeByUserId(hatchetAgent.getAgentNo());
			HatchetMoneyChange moneyChange1 = new HatchetMoneyChange();
			// 提现中
			moneyChange1.setUserId(hatchetAgent.getAgentNo());
			moneyChange1.setSTATUS("10B");
			BigDecimal tixian1 = moneyChangeService.selectTixianSumMoney(moneyChange1);
			if (tixian1 == null) {
				tixian1 = new BigDecimal("0");
			}
			// 提现通过
			moneyChange1.setUserId(hatchetAgent.getAgentNo());
			moneyChange1.setSTATUS("10C");
			BigDecimal tixian2 = moneyChangeService.selectTixianSumMoney(moneyChange1);
			if (tixian2 == null) {
				tixian2 = new BigDecimal("0");
			}

			BigDecimal ktMoney = sumFenrun.subtract(tixian1).subtract(tixian2);
			// 修改用户余额
			hatchetAgent.setTotalMoney(ktMoney);
			hatchetAgent.setUpdateTime(new Date());
			int a = agentService.updateHatchetAgent(hatchetAgent);
			if (a > 0) {

			} else {
				System.out.println(hatchetAgent.getAgentNo());
			}
		}
		List<HatchetMerchant> hatchetMerchants = merchantService.selectAllMerchant();
		for (HatchetMerchant hatchetMerchant : hatchetMerchants) {
			// 总分润
			BigDecimal sumFenrun = moneyChangeService.selectSumFenRenChangeByUserId(hatchetMerchant.getMerchantNo());
			HatchetMoneyChange moneyChange1 = new HatchetMoneyChange();
			// 提现中
			moneyChange1.setUserId(hatchetMerchant.getMerchantNo());
			moneyChange1.setSTATUS("10B");
			BigDecimal tixian1 = moneyChangeService.selectTixianSumMoney(moneyChange1);
			if (tixian1 == null) {
				tixian1 = new BigDecimal("0");
			}
			// 提现通过
			moneyChange1.setUserId(hatchetMerchant.getMerchantNo());
			moneyChange1.setSTATUS("10C");
			BigDecimal tixian2 = moneyChangeService.selectTixianSumMoney(moneyChange1);
			if (tixian2 == null) {
				tixian2 = new BigDecimal("0");
			}

			BigDecimal ktMoney = sumFenrun.subtract(tixian1).subtract(tixian2);
			// 修改用户余额
			hatchetMerchant.setTotalMoney(ktMoney);
			hatchetMerchant.setUpdateTime(new Date());
			int a = merchantService.updateHatchetMerchant(hatchetMerchant);
			if (a < 0) {
				System.out.println(hatchetMerchant.getMerchantNo());
			}

		}
		params.put("msg", "修改成功");
		params.put("status", BusinessStatus.SUCCESS);
		return params;

	}

}
